package com.lebang.controller.sys.cences;

import com.alibaba.fastjson.JSONObject;
import com.lebang.common.Login;
import com.lebang.domain.CencesSdkErrorLog;
import com.lebang.example.CencesSdkErrorLogExample;
import com.lebang.service.cences.CencesSdkErrorLogService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;


/**
 * @author ljx
 * @date 2020-12-08
 */
@Controller
@RequestMapping("/sys/cencessdkerrorlog")
public class CencesSdkErrorLogController {
    @Autowired
    private CencesSdkErrorLogService baseService;

    @Login
    @GetMapping("/list")
    public String list() {
        return "/cencessdkerrorlog/list";
    }

    @Login
    @GetMapping("/look/list")
    public String lookList(Integer time,Model model)
    {
        model.addAttribute("time",time);
        return "/cencessdkerrorlog/lookList";
    }
    @Login
    @GetMapping("/error/list")
    public String errorList(Integer time,String sdkKey,String errorCode,String errorMsg,Model model)
    {
        model.addAttribute("time",time);
        model.addAttribute("sdkKey",sdkKey);
        model.addAttribute("errorCode",errorCode);
        model.addAttribute("errorMsg",errorMsg);
        return "/cencessdkerrorlog/errorList";
    }

    @Login
    @GetMapping("/add")
    public String add(Integer id, Model model) {
        CencesSdkErrorLog vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new CencesSdkErrorLog();
        }
        model.addAttribute("item", vo);
        return "/cencessdkerrorlog/edit";
    }


    @Login
    @PostMapping("/edit")
    @ResponseBody
    public JsonData edit(CencesSdkErrorLog vo) {
        if (vo.getId() != null) {
            baseService.updateById(vo);
        } else {
            vo.setCreateTime(LocalDateTime.now());
            baseService.insertModel(vo);
        }
        return Callback.success("success", "操作成功");
    }

    @Login
    @PostMapping("/delete")
    @ResponseBody
    public JsonData delete(Integer id,Integer time) {
        baseService.deleteByTime(time);
        return Callback.success("success", "操作成功");
    }

    @Login
    @ApiOperation(value = "分页")
    @PostMapping("/list")
    @ResponseBody
    public JSONObject list(HttpServletRequest request, @RequestParam Integer page, @RequestParam Integer limit,Integer star, Integer end) {
        page=(page - 1) * limit;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("data", baseService.selectTime(page,limit,star,end));
        jsonObject.put("count",baseService.countTime(page,limit,star,end));
        return jsonObject;
    }

    @Login
    @ApiOperation(value = "分页")
    @PostMapping("/look/list")
    @ResponseBody
    public JSONObject lookList(HttpServletRequest request,
                               @RequestParam Integer page,
                               @RequestParam Integer limit,
                               Integer time) {
        page=(page-1)*limit;

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("data", baseService.getLookList(page,limit,time));
        jsonObject.put("count", baseService.countLookList(time));
        return jsonObject;
    }

    @Login
    @ApiOperation(value = "分页")
    @PostMapping("/error/list")
    @ResponseBody
    public JSONObject errorList(HttpServletRequest request,
                                @RequestParam Integer page,
                                @RequestParam Integer limit,
                                Integer time, String sdkKey,String errorCode,String errorMsg) {
        page=(page-1)*limit;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("data",baseService.getErrorLookList(page,limit,time,sdkKey,errorCode,errorMsg));
        jsonObject.put("count", baseService.countErrorLookList(time,sdkKey,errorCode,errorMsg));
        return jsonObject;
    }

    @Login
    @PostMapping("/lastCheck")
    @ResponseBody
    public String cen( String date) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate end=LocalDate.parse(date, fmt);
        baseService.starDataInit(LocalDateTime.of(end.plusDays(1), LocalTime.MIN));
        return "success";
    }
}
